$(function(){
	vm.queryGoodsList();
	vm.queryGoodsCount();
	vm.gnMessageList();
	$.gnwindows.gnnotice(1,'<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;"><img src="http://y2.ifengimg.com/cmpp/2014/03/10/04/99a59191-8550-4e77-929d-3c31e25c2192.jpg"/></div>');//通告 1显示/0关闭
	//$.gnwindows.gnshowQrcode(1,"http://orwj42t49.bkt.clouddn.com/1502208297%281%29.png");// 1显示/0关闭
	//$.gnwindows.gnImgCntWindow(1);// 1显示/0关闭
	/*if(v.login_token != 'login_ok'){
		v.login();//若未登录弹出登录框
	}*/
});

/**
 * 前端xss处理
 * */
function safeStr(str){
	return str.replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g, "&quot;").replace(/'/g, "&#039;");
}	

var vm = new Vue({
	el:'#vwrap',
	data:{
		login_token:'',
		goodsList:[],
		goodsCount:0,
		messageList:[],
		commentCount:0,
		messageCount:0,
		searchCnt:''
	},
	methods: {
		login:function(){
			console.log("login_token message:"+v.login_token);
			if(v.login_token=='' || v.login_token == undefined){
				$.gnwindows.gnLoginWindow();return;
			}else{
				vm.gnUserMessage();
			}
		},
		queryGoodsList: function (s) {
			var url= !s ? "../goods/all" : "../goods/all?s="+s;
			console.log("goodsall url="+url);
			//加载层-默认风格
			layer.load('.i-lists');
			$.get(url,"",function(r){
				if(r){layer.closeAll('loading');}
				vm.goodsList=r;
			});
		},
		queryGoodsCount: function () {
			var url= "../goods/count";
			$.get(url,"",function(r){
				if(r.code==0){
					console.log("goodsCount:"+r.count);
					vm.goodsCount=r.count;
				}
			});
		},
		updateGoodsReadCount:function(id){
			console.log("goodsid="+id);
			var url="/WX-PLATFORM/goods/readcount.update?id="+id;
			$.get(url,"",function(r){
				console.log(r);
			});
		},
		showPwd:function(){
			console.log("打赏 login_token message:"+v.login_token);
			if(v.login_token=='' || v.login_token == undefined){
				$.gnwindows.gnLoginWindow();return;
			}
			var flag=false;//判断该项是否已经支付完成！
			if(!flag){
			  layer.alert('打赏获取密码', {
			    shade:0.3,
			   // icon: 0,
			    time:2000000,
			    shift:2,
			    closeBtn:1,
			    shadeClose:true,
			    btn: ['打赏'],
			    btn1:function(index){
			    	
//			  		window.open("<c:url value='/user/pay.htm'/>");
			  		location.href="../user/pay.htm";
			  		layer.close(index);
			  	}
			  });
			}else{
				$(this).text("2s5d");
			}
		},
		gnMessageList:function(){
			var url="../rest/user.message.find.list/"+v.uid;
			$.ajax({
				 url: url,
	             type: "get",
	             dataType: "json",
	             success: function(r){
	            	 vm.messageList=r;
	            	 messageCount=r.length;
	             },
	             error:function(r){
	            	 console.log(r);
	             }
	         });
		},
		//添加用户留言
		gnUserMessage:function(){
			var message={
				wordCount:[],
				msgCnt : "",
				msgCntFn : function(e){
					var messageCnt=$.trim($(".write-message").html());
					/*if(messageCnt.indexOf("&lt;script&gt;")>-1){
						layer.msg("输入的内容中不能存在类似<script>标签",{time:1500});return;
					}*/
					if(messageCnt == '' || messageCnt == undefined){
						layer.msg("请输入留言内容",{time:1500});
						$(".write-message").css({"border":"1px solid red"});
						return;
					}
					$(".comm-null-wrap").remove();
					$(".message-w-box").prepend(
						"<div class='message-cnt-area "+animate.animateStyle[Math.floor(Math.random()*animate.animateStyle.length)]+"'>"+
							"<div class='reply-status'>等待站长回复</div>"+
								"<div class='my-message-info'>"+
									"<p class='my-title' style='text-align: left;'>我的留言：</p>"+
									"<div class='msg-cnt'>"+messageCnt+"</div>"+
									"<p style='text-align: right;color: #B5B5B5;'>"+new Date(Date.parse( new Date())).Format("yyyy-MM-dd hh:mm:ss")+"</p>"+
								"</div>"+
							"</div>"+
						"</div>"
					);
					$(".u-message .write-message").html("");
					var url="../rest/user.message.save";
					$.ajax({
			             type: "post",
			             url: url,
			             data: {"content":messageCnt},
			             dataType: "json",
			             success: function(r){
			            	 console.log(r);
								if(r.code == 0){
									layer.msg("留言成功",{time:1000});
								}
			             }
			           });
				}
			};
			message.msgCntFn();
			
		},
		goodsDetail:function(r){
			location.href="./goods/"+r;
		},
		addMessage:function(){
			console.log("ssssss");
		},
		queryAndAddComment: function(id){
			if(v.login_token == 'login_ok'){
				location.href="../user/comment.htm/"+id;
			}else{
				v.login();
			}
			
		},
		searchJiekou:function(cnt){
			var sdata=safeStr($.trim(this.searchCnt));
			this.queryGoodsList(sdata);
		}
	},
	created :function (){
		
	}
});